package cn.xorange.commons.system.controller.system;

import cn.xorange.commons.core.basic.bean.ResultBean;
import cn.xorange.commons.core.basic.bean.SystemCode;
import cn.xorange.commons.core.basic.controller.DataController;
import cn.xorange.commons.security.annotation.Authorize;
import cn.xorange.commons.system.entity.system.SysOrg;
import cn.xorange.commons.system.service.System.ISysOrgService;
import cn.xorange.commons.core.constants.SysConstants;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @author : yangjian
 * date : 2024/8/12
 * description :
 */
@RestController
@Authorize(hasPerm = "system:org")
@RequestMapping("/system/org")
public class SysOrgController extends DataController<SysOrg, ISysOrgService> {

    @GetMapping("/list/manage")
    public ResultBean getManageOrgList(){
        return ResultBean.onSuccess(this.service.getManageOrgList());
    }

    @Authorize(hasPerm = "add")
    @PostMapping()
    @Transactional(rollbackFor = RuntimeException.class)
    public ResultBean add(@Validated @RequestBody SysOrg sysOrg){
        if(Objects.isNull(sysOrg.getOrgName())){
            return ResultBean.onFail(SystemCode.SYSTEM_PARAM_EXCEPTION);
        }

        int orgNameUnique = this.service.checkOrgNameUnique(sysOrg);
        if(orgNameUnique>0){
            return ResultBean.onFail("同级存在相同部门名");
        }
        return ResultBean.onSuccess(this.service.add(sysOrg));
    }


    @Override
    @Authorize(hasPerm = "edit")
    @PutMapping()
    @Transactional(rollbackFor = RuntimeException.class)
    public ResultBean update(@Validated @RequestBody SysOrg sysOrg){
        if(Objects.isNull(sysOrg.getOrgId()) || Objects.isNull(sysOrg.getParentId()) || Objects.isNull(sysOrg.getTreeNode()) || Objects.isNull(sysOrg.getOrgName())){
            return ResultBean.onFail(SystemCode.SYSTEM_PARAM_EXCEPTION);
        }

        int orgNameUnique = this.service.checkOrgNameUnique(sysOrg);
        if(orgNameUnique>0){
            return ResultBean.onFail("同级存在相同部门名");
        }

        SysOrg originOrg = this.service.getById(sysOrg.getOrgId());

        if (SysConstants.FLAG_DISABLE.equals(sysOrg.getStatus())) {
            int n = service.selectNormalChildrenDeptById(sysOrg.getOrgId());
            if(n>0){
                return ResultBean.onFail("该部门包含未停用的子部门！");
            }
        }
        if(SysConstants.FLAG_DISABLE.equals(originOrg.getStatus())&& SysConstants.FLAG_SUCCESS.equals(sysOrg.getStatus())){
            //状态由停用改为启用，修改父部门状态为启用
            this.service.updateParentDeptStatusNormal(sysOrg);
        }
        return ResultBean.onSuccess(this.service.update(sysOrg));
    }


    @Override
    @Authorize(hasPerm = "remove'")
    @DeleteMapping("/{orgId}")
    @Transactional(rollbackFor = RuntimeException.class)
    public ResultBean del(@PathVariable Long orgId) {
        if (service.hasChildByDeptId(orgId)) {
            return ResultBean.onFail("存在下级部门,不允许删除");
        }
        if (service.checkDeptExistUser(orgId)) {
            return ResultBean.onFail("部门存在用户,不允许删除");
        }
        return ResultBean.onSuccess(this.service.deleteById(orgId));
    }




}
